package com.integralPerformance.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.integralPerformance.handler.excelHandler.ServiceSheetWriteHandler;
import com.integralPerformance.listener.ExcelResProp;
import com.integralPerformance.pojo.entity.ServiceBase;
import com.integralPerformance.pojo.excel.ServiceInfoExcel;
import com.integralPerformance.service.impl.ServiceBaseServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/s2/template")
@Tag(name = "公共服务积分表格模板生成器", description = "表格模板生成器")
public class TemplateController {

    @Autowired
    ServiceBaseServiceImpl serviceBaseService;


    @Operation(summary = "教师公共服务积分填写模板导出",description = "教师公共服务积分填写模板导出")
    @SneakyThrows(IOException.class)
    @GetMapping("/exportTemplateService")
    public void exportInfo(HttpServletResponse response){

        List<ServiceBase> list = serviceBaseService.list();
        List<String> collect = list.stream().map(ServiceBase::getName).collect(Collectors.toList());

        ServiceSheetWriteHandler serviceSheetWriteHandler = new ServiceSheetWriteHandler();
        serviceSheetWriteHandler.setServiceBase(collect);

        ExcelResProp.setExcelRespProp(response, "教师公共服务积分填写模板导出");

        EasyExcel.write(response.getOutputStream(), ServiceInfoExcel.class)
                .registerWriteHandler(serviceSheetWriteHandler)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("填写模板")
                .doWrite(new ArrayList<ServiceInfoExcel>());

    }

}
